package com.tarena.lbs.marketing.web.repository;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.marketing.web.mapper.ActivityMapper;
import com.tarena.lbs.pojo.marketing.po.ActivityPO;
import com.tarena.lbs.pojo.marketing.query.ActivityQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class ActivityRepository {
    @Autowired
    private ActivityMapper activityMapper;
    public PageInfo<ActivityPO> getActivitiesByPage(ActivityQuery query) {
        QueryWrapper queryWrapper=new QueryWrapper();
        if (StringUtils.isNotBlank(query.getActivityName())){
            queryWrapper.like("activity_name",query.getActivityName());
        }
        if (query.getType()!=null){
            queryWrapper.eq("activity_type",query.getType());
        }
        if (query.getBusinessId()!=null){
            queryWrapper.eq("business_id",query.getBusinessId());
        }
        if (query.getStatus()!=null){
            queryWrapper.eq("status",query.getStatus());
        }
        if (query.getDeliveryChannel()!=null){
            queryWrapper.eq("delivery_channel",query.getDeliveryChannel());
        }
        if (StringUtils.isNotBlank(query.getStartDate())&&StringUtils.isNotBlank(query.getEndDate())){
            queryWrapper.between("create_at",query.getStartDate(),query.getEndDate());
        }
        queryWrapper.orderByDesc("create_at");
        //分页查询 转化封装
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        List<ActivityPO> pos = activityMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    public void saveActivity(ActivityPO poParam) {
        activityMapper.insert(poParam);
    }

    public List<ActivityPO> getActivitiesByStoreIds(List<Integer> storeIds) {
        return activityMapper.selectByStoreIds(storeIds);
    }

    public ActivityPO getActivityById(Integer id) {
        return activityMapper.selectById(id);
    }
}
